<?php
class Jnd_Form_Decorator_Formulairejnd extends Zend_Form_Decorator_Abstract {

    public function buildLabel() {

        $element = $this->getElement();
        if( ! ($element instanceof Zend_Form_Element_Submit || $element instanceof Zend_Form_Element_Hash) ) {
            if( isset($element->noLabel) && ! $element->noLabel ) {
                $label = $element->getLabel();
                if( $translator = $element->getTranslator() ) {
                    $label = $translator->translate($label);
                }
                $required = $this->buildRequired();
                return $required . $element->getView()->formLabel($element->getName(), $label, array(
                    "class" => "styleJnd"
                ));
            }
        }
        return '';
    }

    public function buildInput() {

        $element = $this->getElement();
        $helper = $element->helper;
        if( $element->getBelongsTo() !== null ) {
            $nom = $element->getBelongsTo() . '[' . $element->getName() . "]";
        } else {
            $nom = $element->getName();
        }
        return $element->getView()->$helper($nom, $element->getValue(), $element->getAttribs(), $element->options);
    }

    public function buildErrors() {

        $element = $this->getElement();
        $messages = $element->getMessages();
        if( empty($messages) ) {
            return '';
        }
        return '<div class="errors">' . $element->getView()->formErrors($messages) . '</div>';
    }

    public function buildDescription() {

        $element = $this->getElement();
        $desc = $element->getDescription();
        if( empty($desc) ) {
            return '';
        }
        return '<span style="font-size: smaller" class="gris1">' . $desc . '</span>';
    }

    public function buildRequired() {

        $element = $this->getElement();
        if( ! ($element instanceof Zend_Form_Element_Submit || $element instanceof Zend_Form_Element_Hash) ) {
            if( $element->isRequired() ) {
                return '<img src="/images/obligatoire.gif" alt="requis" border="0" class="oligatoire"/>';
            }
        }
        return '';
    }

    public function render( $content ) {

    	$view = Jnd_App::getResource("view");
        $view->headLink()->appendStylesheet($view->baseUrl('css/formulaire.css'));
        $element = $this->getElement();
        if( ! $element instanceof Zend_Form_Element ) {
            return $content;
        }
        if( null === $element->getView() ) {
            return $content;
        }
        $separator = $this->getSeparator();
        $placement = $this->getPlacement();
        if( ! $element instanceof Zend_Form_Element_Hidden ) {
            $label = $this->buildLabel();
            $input = $this->buildInput();
            $errors = $this->buildErrors();
            $desc = $this->buildDescription();
            $output = '<div class="champ">' . $label . '<div class="unchamp">' . $input . $errors . $desc . '</div></div>' . $separator;
            switch( $placement ) {
                case (self::PREPEND):
                    return $output . $separator . $content;
                case (self::APPEND):
                default:
                    return $content . $separator . $output;
            }
        } else {
            return $content . $separator . $this->buildInput();
        }
    }
}
